//
//  DownloadItemView.swift
//  YoruNeko
//
//  Created by liphone on 2021/6/10.
//

import SwiftUI



struct DownloadItemView: View {
    
    @State private var isEditing = false
    var dataModel:DownloadViewItem
    
    var progressView = YRNKCycleStyleProgressView(radius: 15, boldValue: 3, color: .black.withAlphaComponent(0.7))
    
<<<<<<< HEAD:YoruNeko/YoruNeko/view-controller/download-scene/component/DownloadItemView.swift
    
    //function closure
    //视图无需掌管状态，只需要知道按下了一次按钮即可，待数据源被修改后，视图随即更新
    
    /**
     绑定状态控制按钮方法，传入该视图的id来辨识调用的哪个项
     */
    var onStateControllBtnPress: ((Int)->Void)?
    /**
     绑定视图的点击方法
     */
    var onTrashBtnPress: ((Int)->Void)?
    var onItemPress: ((Int)->Void)?
    
    
    
=======
>>>>>>> c61751e091897e535a726d3455f2deada7031433:YoruNeko/YoruNeko/view-controller/download-scene/DownloadItemView.swift
    var body: some View {
        HStack{
            //group progressView and
            ZStack{
                //这里有个很奇怪的设置，感觉就像计算值传递，到时问下老师
                progressView.frame(width: progressView.frame.width, height: progressView.frame.height, alignment: /*@START_MENU_TOKEN@*/.center/*@END_MENU_TOKEN@*/)
                Group{
                    switch dataModel.state {
                    case .finished:
                        Button(action: onProgressButtonPressed){
                            Label("", systemImage: "checkmark")
                        }.foregroundColor(.black.opacity(0.7))
                    case .going:
                        Button(action: onProgressButtonPressed){
                           Label("", systemImage: "pause")
                        }.foregroundColor(.black.opacity(0.7))
                    case .paused:
                        Button(action: onProgressButtonPressed){
                            Label("", systemImage:"arrow.down.to.line")
                        }.foregroundColor(.black.opacity(0.7))
                    case .stopped:
                        Button(action: onProgressButtonPressed){
                            Label("", image: "xmark").foregroundColor(.red)
                        }.foregroundColor(.black.opacity(0.7))
                        
                    }
                }.frame(width: progressView.frame.width, height: progressView.frame.height, alignment: /*@START_MENU_TOKEN@*/.center/*@END_MENU_TOKEN@*/).padding(.leading,7).padding(.bottom,2)
                
            }
            
            VStack(alignment: .leading){
                Text(dataModel.title)
                //设置项目描述文本
                Text(getSubTitleDescription()).font(.subheadline).foregroundColor(.gray)
                //设置走马灯省略
                    .lineLimit(1)
            }
            Spacer()
            Button(action: {
                onTrashBtnPress?(dataModel.id.hashValue)
            }, label: {
                Image(systemName: "trash.fill")
            }).foregroundColor(.black.opacity(0.7)).scaleEffect(x: 1.5, y: 1.5, anchor: /*@START_MENU_TOKEN@*/.center/*@END_MENU_TOKEN@*/)
        }
<<<<<<< HEAD:YoruNeko/YoruNeko/view-controller/download-scene/component/DownloadItemView.swift
        .contentShape(Rectangle()) //respond to hit test
        .onTapGesture(count: /*@START_MENU_TOKEN@*/1/*@END_MENU_TOKEN@*/, perform: {
            onItemPress?(dataModel.id.hashValue)
        })
=======
        
        
>>>>>>> c61751e091897e535a726d3455f2deada7031433:YoruNeko/YoruNeko/view-controller/download-scene/DownloadItemView.swift
    }
    
    //进度条下的按钮
    func onProgressButtonPressed(){
        onStateControllBtnPress?(dataModel.id.hashValue)
    }
    
    private func getSubTitleDescription()->String{
       
        return "\(YRNKUtil.getSuitableFileSizeString(size: dataModel.downloadedSize))\\\(YRNKUtil.getSuitableFileSizeString(size: dataModel.totalSize))  \(dataModel.getStateDescriptionByState())"
    }
}

struct DownloadItemView_Previews: PreviewProvider {
    static var previews: some View {
        DownloadItemView(dataModel: DownloadViewItem("picture", urlStr: "www.hgc.ink", contentLength: 4400000))
    }
}
